#pragma once

#include <iostream>
#include <list>
#include <assert.h>

using namespace std;

namespace TEST
{
	template<class T>
	struct ListNode
	{
		ListNode<T>* _next;
		ListNode<T>* _prev;
		T	         _data;

		ListNode(const T& value = T())
			: _next(nullptr)
			, _prev(nullptr)
			, _data(value)
		{}
	};

	template<class T, class Referance, class Ptr>
	struct __list_iterator
	{
		typedef ListNode<T> Node;
		typedef __list_iterator<T, Referance, Ptr> self;

		Node* _node;

		__list_iterator(Node* node)
			: _node(node)
		{}

		//++it
		self& operator++ ()
		{
			_node = _node->_next;
			return *this;
		}
		//it++
		self operator++ (int)
		{
			self temp(_node);
			_node = _node->_next;
			return temp;
		}
		//--it
		self& operator-- ()
		{
			_node = _node->_prev;
			return *this;
		}
		//it--
		self operator-- (int)
		{
			self temp(_node);
			_node = _node->_prev;
			return temp;
		}
		Referance operator* ()
		{
			return _node->_data;
		}
		Ptr operator-> ()
		{
			return &_node->_data;
		}
		bool operator!= (const self& value)
		{
			return _node != value._node;
		}
		bool operator == (const self& value)
		{
			return _node == value._node;
		}
	};

	template<class T, class Referance, class Ptr>
	struct __list_reverse_iterator
	{
		typedef ListNode<T> Node;
		typedef __list_reverse_iterator<T, Referance, Ptr> self;

		Node* _node;

		__list_reverse_iterator(Node* node)
			: _node(node)
		{}

		//++it
		self& operator++ ()
		{
			_node = _node->_prev;
			return *this;
		}
		//it++
		self operator++ (int)
		{
			self temp(_node);
			_node = _node->_prev;
			return temp;
		}
		//--it
		self& operator-- ()
		{
			_node = _node->_next;
			return *this;
		}
		//it--
		self operator-- (int)
		{
			self temp(_node);
			_node = _node->_next;
			return temp;
		}
		Referance operator* ()
		{
			return _node->_data;
		}
		Ptr operator-> ()
		{
			return &_node->_data;
		}
		bool operator!= (const self& value)
		{
			return _node != value._node;
		}
		bool operator== (const self& value)
		{
			return _node == value._node;
		}
	};

	template<class T>
	class list
	{
		typedef ListNode<T> Node;

	public:
		typedef __list_iterator<T, T&, T*> iterator;
		typedef __list_iterator<T, const T&, const T*> const_iterator;
		typedef __list_reverse_iterator<T, T&, T*> reverse_iterator;
		typedef __list_reverse_iterator<T, const T&, const T*> const_reverse_iterator;

		list()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		list(list<T>& it)
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			for (const auto e : it)
			{
				push_back(e);
			}
		}

		list<T>& operator= (list<T> it)
		{
			swap(it);

			return *this;
		}


		void push_back(const T& value = T())
		{
			//Node* newNode = new Node(value);

			//Node* tail = _head->_prev;
			//tail->_next = newNode;
			//newNode->_prev = tail;
			//newNode->_next = _head;
			//_head->_prev = newNode;

			insert(end(), value);
		}

		void pop_back()
		{
			//assert(begin() != end());

			//Node* tail = _head->_prev;
			//Node* newTail = tail->_prev;

			//_head->_prev = newTail;
			//newTail->_next = _head;

			//delete tail;

			erase(--end());
		}

		void push_front(const T& value = T())
		{
			insert(begin(), value);
		}

		void pop_front()
		{
			erase(begin());
		}

		iterator erase(iterator pos)
		{
			assert(begin() != end());

			Node* curNode = pos._node;
			Node* prevNode = curNode->_prev;
			Node* nextNode = curNode->_next;

			nextNode->_prev = prevNode;
			prevNode->_next = nextNode;
			delete curNode;

			return nextNode;
		}

		iterator insert(iterator pos, const T& value = T())
		{
			Node* newNode = new Node(value);
			Node* prevNode = pos._node->_prev;
			Node* curNode = pos._node;

			//prevNode newNode curNode;
			prevNode->_next = newNode;
			newNode->_prev = prevNode;
			newNode->_next = curNode;
			curNode->_prev = newNode;

			return newNode;
		}

		void swap(list<T>& it)
		{
			std::swap(_head, it._head);
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}

		iterator begin()
		{
			return _head->_next;
		}

		const_iterator begin() const
		{
			return _head->_next;
		}

		iterator end()
		{
			return _head;
		}

		const_iterator end() const
		{
			return _head;
		}

		reverse_iterator rbegin()
		{
			return _head->_prev;
		}

		const_reverse_iterator rbegin() const
		{
			return _head->_prev;
		}

		reverse_iterator rend() 
		{
			return _head;
		}
		
		const_reverse_iterator rend() const
		{
			return _head;
		}

		~list()
		{
			clear();

			delete _head;
			_head = nullptr;
		}

	private:
		Node* _head;
	};


	//void Test1()
	//{
	//	list<int> lt;
	//	lt.push_back(1);
	//	lt.push_back(2);
	//	lt.push_back(3);
	//	lt.push_back(4);

	//	list<int>::reverse_iterator rit = lt.rbegin();
	//	while (rit != lt.rend())
	//	{
	//		cout << *rit << " ";
	//		rit++;
	//	}
	//	cout << endl;

	//	//lt.insert(++lt.begin(), 11);
	//	//lt.insert(--lt.end(), 22);

	//	//lt.pop_back();
	//	//lt.pop_back();

	//	//lt.push_front(22);
	//	//lt.pop_front();

	//	//lt.clear();


	//	//for (auto& e : lt)
	//	//	cout << e << " ";
	//	//cout << endl;

	//}

	//void Test1()
	//{
	//	list<int> lt;
	//	lt.push_back(1);
	//	lt.push_back(2);
	//	lt.push_back(3);
	//	lt.push_back(4);
	//	lt.push_back(5);

	//	//list<int> lt2;
	//	//for (int i = 0; i < 10; i++)
	//	//	lt2.push_back(i + 1);

	//	//lt.swap(lt2);

	//	//for (auto e : lt)
	//	//	cout << e << " ";
	//	//cout << endl;


	//	list<int>::const_iterator it = ((const list<int>)lt).begin();

	//	while (it != ((const list<int>)lt).end())
	//	{
	//		cout << *it << " "; 
	//		it++;
	//	}
	//	cout << endl;

	//}

	void Test1()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		lt.push_back(5);
	}
}
